<?php

/**
 * Created by PhpStorm.
 * User: suaxu
 * Date: 2017/5/31
 * Time: 11:33
 */
namespace Controller\Api;

class Ipinfo extends \Controller\Api
{

    public function get()
    {
        include(SP . "class/utils/geoip/geoip.inc.php");
        $gi = geoip_open(SP . "class/utils/geoip/GeoIP.dat", GEOIP_STANDARD);
        $ip = $this->getIP();
        $country_code = geoip_country_code_by_addr($gi, $ip);
        $country_name = geoip_country_name_by_addr($gi, $ip);
        geoip_close($gi);
        $jsonEcho = array();
        $jsonEcho["error"] = 0;
        $jsonEcho["country_code"] = $country_code;
        $jsonEcho["country_name"] = $country_name;
        echo json_encode($jsonEcho);
        exit;
    }

    public function post()
    {
    }

    public function getIP()
    {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('HTTP_X_FORWARDED')) {
            $ip = getenv('HTTP_X_FORWARDED');
        } elseif (getenv('HTTP_FORWARDED_FOR')) {
            $ip = getenv('HTTP_FORWARDED_FOR');

        } elseif (getenv('HTTP_FORWARDED')) {
            $ip = getenv('HTTP_FORWARDED');
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

}

